﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.workPlan
{
    public partial class WorkPlanEva : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["workplan_id"]))
                {
                    Maticsoft.BLL.workplan wm = new Maticsoft.BLL.workplan();
                    lcs.BLL.user um = new BLL.user();

                    int workplan_id = int.Parse(Request["workplan_id"]);

                    Maticsoft.Model.workplan w = wm.GetModel(workplan_id);
                    string user_name = um.GetModel(w.submit_uid.Value).user_name;


                    //this.lbl_content.Text = w.workplan_content;
                    this.lbl_info.Text = "提交人：" + user_name + "，提交时间：" + w.submit_time + ",类型：" + w.workplan_type;
                    this.lbl_title.Text = w.workplan_title;

                    DataSet ds_s = new Maticsoft.BLL.workplan_detail().GetList(" workplan_id= " + workplan_id + " order by orderId  ");
                    this.GridView1.DataSource = ds_s;
                    this.GridView1.DataBind();


                    if (Session["login"] != null)
                    {
                        DataSet ds = Session["login"] as DataSet;

                        int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());

                        if (roles_id == 1024)
                        {
                            //班长

                        }
                        if (roles_id == 1025)
                        {
                            //绩效专责
                            this.Panel1.Visible = false;
                        }
                        if (roles_id == 1026)
                        {
                            //部门经理
                        }
                    }
                }
            }
        }

        //提交
        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Maticsoft.BLL.workplan wm = new Maticsoft.BLL.workplan();
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());
                int user_depid = 0;
                if (!string.IsNullOrEmpty(ds.Tables[0].Rows[0]["user_depid"].ToString()))
                {
                    user_depid = Convert.ToInt32(ds.Tables[0].Rows[0]["user_depid"].ToString());
                }

                if (roles_id == 1024)
                {
                    //班长
                    if (!string.IsNullOrEmpty(Request["workplan_id"]))
                    {
                        int workplan_id = int.Parse(Request["workplan_id"]);

                        Maticsoft.Model.workplan w = wm.GetModel(workplan_id);

                        w.check_step = "班长评价完成";

                        CheckWorkPlan(wm, user_id, w);
                    }
                }
                if (roles_id == 1025)
                {
                    //绩效专责
                    if (!string.IsNullOrEmpty(Request["workplan_id"]))
                    {
                        int workplan_id = int.Parse(Request["workplan_id"]);

                        Maticsoft.Model.workplan w = wm.GetModel(workplan_id);

                        w.check_step = "绩效专责评价完成";

                        CheckWorkPlan(wm, user_id, w);
                    }

                }
                if (roles_id == 1026)
                {
                    //部门经理
                    if (!string.IsNullOrEmpty(Request["workplan_id"]))
                    {
                        int workplan_id = int.Parse(Request["workplan_id"]);

                        Maticsoft.Model.workplan w = wm.GetModel(workplan_id);

                        w.check_step = "部门经理评价完成";

                        CheckWorkPlan(wm, user_id, w);
                    }

                }

            }
        }

        private void CheckWorkPlan(Maticsoft.BLL.workplan wm, int user_id, Maticsoft.Model.workplan w)
        {
            if (wm.Update(w))
            {
                Maticsoft.BLL.workplan_evaluate wem = new Maticsoft.BLL.workplan_evaluate();
                Maticsoft.Model.workplan_evaluate we = new Maticsoft.Model.workplan_evaluate();

                we.eva_time = DateTime.Now;
                we.eva_uid = user_id;
                we.workplan_id = w.workplan_id;
                string pj = txt_pj.Text;
                we.eva_content = pj;


                if (w.check_step == "班长评价完成")
                {
                    //班长
                    we.eva_userRole = "班长";

                    Maticsoft.BLL.workplan_detail wdm = new Maticsoft.BLL.workplan_detail();
                    Maticsoft.Model.workplan_detail wd = null;

                    for (int i = 0; i < this.GridView1.Rows.Count; i++)
                    {
                        DropDownList ddl_isComp = (DropDownList)GridView1.Rows[i].Cells[0].FindControl("ddl_isComp");
                        Label lbl_workplan_detail_id = (Label)GridView1.Rows[i].Cells[0].FindControl("lbl_workplan_detail_id");

                        wd = wdm.GetModel(int.Parse(lbl_workplan_detail_id.Text));

                        wd.evaStr = pj;

                        wdm.Update(wd);
                    }
                }

                if (w.check_step == "绩效专责评价完成")
                {
                    //绩效专责  
                    we.eva_userRole = "绩效专责";

                    Maticsoft.BLL.workplan_detail wdm = new Maticsoft.BLL.workplan_detail();
                    Maticsoft.Model.workplan_detail wd = null;
                   
                    for (int i = 0; i < this.GridView1.Rows.Count; i++)
                    {
                        DropDownList ddl_isComp = (DropDownList)GridView1.Rows[i].Cells[0].FindControl("ddl_isComp");
                        Label lbl_workplan_detail_id = (Label)GridView1.Rows[i].Cells[0].FindControl("lbl_workplan_detail_id");

                        wd = wdm.GetModel(int.Parse(lbl_workplan_detail_id.Text));

                        if (ddl_isComp.SelectedValue == "是")
                        {
                            wd.isComplete = true;
                        }
                        else
                        {
                            wd.isComplete = false;
                        }

                        wdm.Update(wd);
                    }
                    
                }
                if (w.check_step == "部门经理评价完成")
                {
                    //部门经理
                    we.eva_userRole = "部门经理";

                    Maticsoft.BLL.workplan_detail wdm = new Maticsoft.BLL.workplan_detail();
                    Maticsoft.Model.workplan_detail wd = null;

                    for (int i = 0; i < this.GridView1.Rows.Count; i++)
                    {
                        TextBox txt_score = (TextBox)GridView1.Rows[i].Cells[0].FindControl("txt_score");
                        Label lbl_workplan_detail_id = (Label)GridView1.Rows[i].Cells[0].FindControl("lbl_workplan_detail_id");

                        wd = wdm.GetModel(int.Parse(lbl_workplan_detail_id.Text));

                        decimal score = 0;
                        if (!string.IsNullOrEmpty(txt_score.Text))
                        {
                            try
                            {
                                score = Convert.ToDecimal(txt_score.Text);
                            }
                            catch (Exception)
                            {
                            }
                        }

                        wd.evaScore = score;
                        wd.evaStr = pj;

                        wdm.Update(wd);
                    }
                   
                }
                if (wem.Add(we) > 0)
                {
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this.Page, "提交成功！", "ListWorkPlanEva.aspx");
                }

               
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_score = (Label)e.Row.FindControl("lbl_score");
                TextBox txt_score = (TextBox)e.Row.FindControl("txt_score");
                Label lbl_iscomp = (Label)e.Row.FindControl("lbl_iscomp");
                DropDownList ddl_isComp = (DropDownList)e.Row.FindControl("ddl_isComp");

                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());

                    if (roles_id == 1024)
                    {
                        //班长
                        lbl_score.Visible = false;
                        txt_score.Visible = false;
                        lbl_iscomp.Visible = false;
                        ddl_isComp.Visible = false;
                    }
                    if (roles_id == 1025)
                    {
                        //绩效专责
                        lbl_score.Visible = false;
                        txt_score.Visible = false;
                        lbl_iscomp.Visible = true;
                        ddl_isComp.Visible = true;

                    }
                    if (roles_id == 1026)
                    {
                        //部门经理
                        lbl_score.Visible = true;
                        txt_score.Visible = true;
                        lbl_iscomp.Visible = false;
                        ddl_isComp.Visible = false;
                    }

                }
            }
        }
    }
}